package ywj;

import java.util.*;
import java.util.ArrayList;
import java.lang.Long;

public class Main {
    public static void main(String[] args){
        ArrayList<Web> webs=new ArrayList<>();

        Web web1=new Web("1",0l);
        Web web2=new Web("2",0l);
        Web web3=new Web("3",0l);

        webs.add(web1);
        webs.add(web2);
        webs.add(web3);

        Scanner sc=new Scanner(System.in);

        while(true){

            System.out.println("请输入1 访问页面，输入2 显示访问量排行榜，输入3 统计总访问量，输入4 计算平均访问量，输入5 显示页面最后访问时间，输入6 清空所有页面访问量，输入7 退出");
            int p=sc.nextInt();
            sc.nextLine();

            if(p==1){
                int i=0;

                showwebs(webs);
                System.out.println("请输入您要访问的页面的url:");
                String url=sc.next();

                for (i = 0; i < webs.size(); i++) {
                    if(url.equals(webs.get(i).getUrl())){
                        webs.get(i).setVisitnumber(webs.get(i).getVisitnumber()+1);
                        Date date=new Date();
                        webs.get(i).setDate(date.toString());
                        webs.get(i).showDate();
                        break;
                    }
                }

                if(i==webs.size()){
                    Web web0=new Web(url,0l);
                    webs.add(web0);
                    showwebs(webs);
                }
            }

            if(p==2){
                System.out.println("访问量排行榜为：");
                showwebs(ranklist(webs));
                showwebs(webs);
            }

            if(p==3){
                sumvisit(webs);
            }

            if(p==4){
                avgvisit(webs);
            }

            if(p==5){
                System.out.println("请输入您想查询访问时间的页面的url");
                String url=sc.next();

                for (int i = 0; i < webs.size(); i++) {
                    if(url.equals(webs.get(i).getUrl())){
                        webs.get(i).showDate();
                    }
                }
            }

            if(p==6){
                webs=removevisit(webs);
                System.out.println("清空后的页面为：");
                showwebs(webs);
            }

            if(p==7){
                break;
            }
        }
    }
    static void showwebs(ArrayList<Web> webs){
        System.out.println("当前已有页面为：");
        for (int i = 0; i < webs.size(); i++) {
            Web web=webs.get(i);

            System.out.print("页面url为：");
            System.out.println(web.getUrl());
            System.out.print("页面访问量为：");
            System.out.println(web.getVisitnumber());
            System.out.println();
        }
    }

    static ArrayList<Web> ranklist(ArrayList<Web> webs){
       ArrayList<Web> webs1=new ArrayList<>(webs);
        Web web=new Web();
        for (int i = 0; i < webs1.size(); i++) {
            for(int j=0;j<webs1.size()-i-1;j++){
                if(webs1.get(j).getVisitnumber()<webs1.get(j+1).getVisitnumber()){
                    web=webs1.get(j);
                    webs1.set(j,webs1.get(j+1));
                    webs1.set(j+1,web);
                    showwebs(webs1);
                }
            }
        }
        return webs1;
    }
    static Long sumvisit(ArrayList<Web> webs){
        Long s=0l;
        for (int i = 0; i < webs.size(); i++) {
            s+=webs.get(i).getVisitnumber();
        }
        System.out.println("页面访问量总和为:"+s);
        return s;
    }

    static void avgvisit(ArrayList<Web> webs){
        Long s=sumvisit(webs);
        Long a=s/webs.size();
        System.out.println("页面数为："+webs.size());
        System.out.println("平均页面访问量为："+a);
    }

    static ArrayList<Web> removevisit(ArrayList<Web> webs){
        for (int i = 0; i < webs.size(); i++) {
            webs.get(i).setVisitnumber(0l);
        }
        return webs;
    }


}
